﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace huaxin_core
{
    public class WxAuxiliary
    {

        /// <summary>
        /// 生成订单号
        /// </summary>
        /// <param name="startName"></param>
        /// <returns></returns>
        public static string GetOrderNumber(string startName)
        {
            return string.Format("{0}{1}",startName,Next(1000,1).ToString());//yyyyMMddHHmmssms
        }


        private static int Next(int numSeeds, int length)
        {

            byte[] buffer = new byte[length];

            System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();

            Gen.GetBytes(buffer);

            uint randomResult = 0x0;//这里用uint作为生成的随机数  
            for (int i = 0; i < length; i++)
            {
                randomResult |= ((uint)buffer[i] << ((length - 1 - i) * 8));
            }
            return (int)(randomResult % numSeeds);
        }

        /// <summary>
        /// 订单号防重复
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }

    }
}
